package cn.z2huo.demo.spring.webmvc.controller.uploadfile;

import cn.z2huo.demo.spring.webmvc.entity.dto.No3CsvDataDTO;
import cn.z2huo.demo.spring.webmvc.service.ReadCsvFileServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>测试上传文件
 *
 * @author z2huo
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/uploadFile")
public class UploadFileController {

    private final ReadCsvFileServiceImpl readCsvFileService;

    /**
     * <p>使用 POST 请求仅上传一个文件
     *
     * @param file 文件
     * @return 成功
     */
    @PostMapping("/onlyUpdateFile")
    public String onlyUploadFile(@RequestParam("file") MultipartFile file) {

        String name = file.getName();
        String contentType = file.getContentType();
        String originalFilename = file.getOriginalFilename();
        long size = file.getSize();
        log.info("upload file, " +
                "name: {}, " +
                "contentType: {}, " +
                "originalFilename: {}, " +
                "size: {}", name, contentType, originalFilename, size);

        if (!file.isEmpty()) {
            try {
                List<No3CsvDataDTO> data = readCsvFileService.readNo3CsvFile(file.getInputStream());
                No3CsvDataDTO first = data.getFirst();
                log.info("first data is {}", first);
            } catch (IOException e) {
                log.error("read csv file from request fail", e);
                throw new RuntimeException(e);
            }
        }
        return "upload file success";
    }

    /**
     * <p>使用 POST 请求上传多个文件
     *
     * @param fileList 文件列表
     * @return 成功
     */
    @PostMapping("/batchUploadFile")
    public String batchUploadFile(@RequestParam("file") List<MultipartFile> fileList) {

        log.info("上传多个文件");
        fileList.forEach(file -> {
            String name = file.getName();
            String contentType = file.getContentType();
            String originalFilename = file.getOriginalFilename();
            long size = file.getSize();
            log.info("upload file, " +
                    "name: {}, " +
                    "contentType: {}, " +
                    "originalFilename: {}, " +
                    "size: {}", name, contentType, originalFilename, size);
        });

        if (CollectionUtils.isNotEmpty(fileList)) {

            for (MultipartFile file : fileList) {
                if (!file.isEmpty()) {
                    try {
                        List<No3CsvDataDTO> data = readCsvFileService.readNo3CsvFile(file.getInputStream());
                        No3CsvDataDTO first = data.getFirst();
                        log.info("first data is {}", first);
                    } catch (IOException e) {
                        log.error("read csv file from request fail", e);
                        throw new RuntimeException(e);
                    }
                }
            }
        }

        return "upload file success";
    }

}
